import requests
from bs4 import BeautifulSoup
import time

def fetch_double_color_ball_data(page):
    # 构造请求的URL
    url = f"https://6.17500.cn/?lottery=more&page={page}"

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'Connection': 'keep-alive',
    }
    
    # 发送HTTP请求
    response = requests.get(url, headers=headers)
       
    # 检查请求是否成功
    if response.status_code != 200:
        print(f"获取第{page}页数据失败。状态码: {response.status_code}")
        return None
    
    # 使用BeautifulSoup解析HTML内容
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # 找到包含开奖数据的列表
    data_list = soup.find('ul', class_="lskj_list")
    
    # 如果没有找到列表，返回None
    if not data_list:
        print("页面上未找到数据列表。")
        return None
    
    # 解析列表数据
    list_items = data_list.find_all('li')
    data = []
    
    for item in list_items:
        try:
            # 获取期号和日期
            info_div = item.find('div', class_='info')
            if not info_div:
                continue
                
            info_text = info_div.text.strip()
            period_date_parts = info_text.split()
            
            if len(period_date_parts) >= 2:
                period = period_date_parts[0].replace('期：', '')
                date = period_date_parts[1].replace('开奖日期：', '')
            else:
                continue
            
            # 获取红球号码
            red_balls_div = item.find('div', class_='red_ball')
            if not red_balls_div:
                continue
                
            red_balls = [span.text.strip() for span in red_balls_div.find_all('span')]
            
            # 获取蓝球号码
            blue_ball_div = item.find('div', class_='blue_ball')
            if not blue_ball_div:
                continue
                
            blue_ball = blue_ball_div.find('span').text.strip() if blue_ball_div.find('span') else ""
            
            # 将数据存储为字典
            data.append({
                "期号": period,
                "开奖日期": date,
                "红球号码": red_balls,
                "蓝球号码": blue_ball
            })
        except Exception as e:
            print(f"解析项目时出错: {e}")
            continue
    
    return data

# 示例：抓取第16页的数据
page = 16
result = fetch_double_color_ball_data(page)

if result:
    for item in result:
        print(item)
else:
    print("未获取到数据")